package com.filldream.fastboot.common.constant.enums;

import com.filldream.fastboot.common.entity.db.StockMarket;
import com.filldream.fastboot.common.util.JsonUtil;
import org.apache.commons.lang3.ObjectUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StockMarketUtil {

    private static final String marketJson = "[{\"id\":\"1\",\"marketName\":\"沪A\",\"thsType\":\"17\",\"cfImgType\":\"1\",\"txType\":\"1\",\"codePrefix\":\"sh\",\"remark\":\"个阳\"},{\"id\":\"2\",\"marketName\":\"深A\",\"thsType\":\"33\",\"cfImgType\":\"0\",\"txType\":\"51\",\"codePrefix\":\"sz\",\"remark\":\"个阳\"},{\"id\":\"3\",\"marketName\":\"京A\",\"thsType\":\"151\",\"cfImgType\":\"0\",\"txType\":\"62\",\"codePrefix\":\"bj\",\"remark\":\"个阳\"},{\"id\":\"4\",\"marketName\":\"港股\",\"thsType\":\"177\",\"cfImgType\":\"116\",\"txType\":\"100\",\"codePrefix\":\"hk\",\"remark\":\"个阳\"},{\"id\":\"5\",\"marketName\":\"美股\",\"thsType\":\"185\",\"cfImgType\":\"105\",\"txType\":\"200\",\"codePrefix\":\"us\",\"remark\":\"个阳\"},{\"id\":\"6\",\"marketName\":\"沪指\",\"thsType\":\"16\",\"cfImgType\":\"1\",\"txType\":\"1\",\"codePrefix\":\"sh\",\"remark\":\"沪指\"},{\"id\":\"7\",\"marketName\":\"深指\",\"thsType\":\"32\",\"cfImgType\":\"0\",\"txType\":\"51\",\"codePrefix\":\"sz\",\"remark\":\"深指\"},{\"id\":\"8\",\"marketName\":\"京指\",\"thsType\":\"144\",\"cfImgType\":\"0\",\"txType\":\"62\",\"codePrefix\":\"bj\",\"remark\":\"京指\"},{\"id\":\"9\",\"marketName\":\"港指\",\"thsType\":\"176\",\"cfImgType\":\"100,124\",\"txType\":\"100\",\"codePrefix\":\"hk\",\"remark\":\"港指\"},{\"id\":\"10\",\"marketName\":\"美指\",\"thsType\":\"88\",\"cfImgType\":\"100\",\"txType\":\"200\",\"codePrefix\":\"us\",\"remark\":\"美指\"},{\"id\":\"11\",\"marketName\":\"沪基\",\"thsType\":\"20\",\"cfImgType\":\"1\",\"txType\":\"1\",\"codePrefix\":\"sh\",\"remark\":\"沪基\"},{\"id\":\"12\",\"marketName\":\"深基\",\"thsType\":\"36\",\"cfImgType\":\"0\",\"txType\":\"51\",\"codePrefix\":\"sz\",\"remark\":\"深基\"}]";

    public static List<StockMarket> getStockMarkets() {
        return JsonUtil.fromJosnArray( marketJson, StockMarket.class );
    }

    public static List<String> getThsMarketIdList() {
        return getStockMarkets().stream().map( StockMarket::getThsType ).collect(Collectors.toList());
    }

    public static StockMarket findByTxType(String txType) {
        return getStockMarkets().stream().filter(t -> t.getTxType().equals(txType) ).findFirst().orElse(null);
    }

    public static StockMarket findByThsType(String thsType,String thsCode) {
        List<StockMarket> list = getStockMarkets().stream().filter(t -> t.getThsType().equals(thsType)).collect(Collectors.toList());
        if( ObjectUtils.isNotEmpty(list) ){
            if( thsType.equals("176") ){
                StockSearchCodeEnum stockSearchCodeEnum = StockSearchCodeEnum.findBySearchCode(list.get(0).getCodePrefix() + thsCode, 1);
                if( stockSearchCodeEnum != null ){
                    StockMarket stockMarket = list.stream().filter(t -> t.getCfImgType().contains(stockSearchCodeEnum.getCfImgType())).findFirst().orElse(null);
                    if( stockMarket != null ){
                        stockMarket.setCfImgType(  stockSearchCodeEnum.getCfImgType() );
                        return stockMarket;
                    }
                }
            }
            return list.get(0);
        }
        return null;
    }

    public static StockMarket findByCodePrefix(String codePrefix) {
        return getStockMarkets().stream().filter(t -> t.getCodePrefix().equals(codePrefix) ).findFirst().orElse(null);
    }

    /**
     * 通过腾讯code换取东方财富的code
     * @param searchCode    腾讯code
     * @return  东方财富的code
     */
    public static String getCfImgIdBySearchCode(String searchCode) {
        if( !searchCode.startsWith("us") && searchCode.contains(".")  ){
            return searchCode;
        }
        if(!StockSearchCodeEnum.checkTxCode(searchCode)){
            return "";
        }
        String codePrefix = searchCode.substring(0, 2);
        String simpleCode = searchCode.substring(2).replaceAll("\\.","");
        //确定东财的code,确定指数或特殊股票的代码
        StockSearchCodeEnum stockSearchCodeEnum = StockSearchCodeEnum.findBySearchCode(searchCode, 3);
        if( stockSearchCodeEnum != null ){
            return stockSearchCodeEnum.getCfImgType() + "." + stockSearchCodeEnum.getCfCode();
        }else{
            //个股的代码
            String cfImgMarketType = "";
            List<StockMarket> marketList = getStockMarkets().stream().filter(t -> t.getCodePrefix().equals(codePrefix)).collect(Collectors.toList());
            if(ObjectUtils.isNotEmpty(marketList)){
                cfImgMarketType = marketList.get(0).getCfImgType();
            }
            return cfImgMarketType + "." + simpleCode;
        }
    }


    /**
     * 获取同花顺分时图的code
     * @param searchCode    腾讯code
     * @return  同花顺分时图的code
     */
    public static String getThsKLineCode(String searchCode) {
        searchCode = searchCode.replaceAll("\\.","");
        StockSearchCodeEnum stockSearchCodeEnum = StockSearchCodeEnum.findBySearchCode(searchCode, 3);
        if( stockSearchCodeEnum != null ){
            return stockSearchCodeEnum.getThsKLineType() + "_" + stockSearchCodeEnum.getThsCode();
        }
        if( searchCode.startsWith("sh") || searchCode.startsWith("sz") ){
            return "hs_" + searchCode.substring(2);
        }
        if( searchCode.startsWith("hk") ){
            String[] split = searchCode.split("hk");
            String thsCode = split[1];
            if( thsCode.startsWith("0")  && thsCode.length() == 5){
                thsCode = thsCode.substring(1);
            }
            return "hk_HK" + thsCode;
        }
        if( searchCode.startsWith("us") ){
            return searchCode.replaceAll("us", "usa_");
        }
        return searchCode;
    }


}
